"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具显示路径下文件信息
Create At   : 2023/4/12
Owner       : opentestcase019
Description :
    1.显示路径下文件信息，指定正常路径
    2.显示路径下文件信息，指定错误路径
    3.显示路径下文件信息，不指定-p path参数
    4.显示路径下文件信息，指定-m measure_type参数为正常值
    5.显示路径下文件信息，指定-m measure_type参数为非法值
    6.显示路径下文件信息，指定-U UDS:socket_domain参数为正常值
    7.显示路径下文件信息，指定指定错误的-U UDS:socket_domain参数和权限不足的socket_domain
    8.显示路径下文件信息，同时指定-m,-U参数
    9.在集群停止的情况下执行
Expect      :
    1.显示成功
    2.合理报错
    3.合理报错
    4.显示成功
    5.合理报错measure_type error.
    6.显示成功
    7.合理报错Failed to get uds connection.
    8.显示成功
    9.合理报错
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common()
        self.constant = Constant()

    def test_sharestorage_tool(self):
        step1 = "-----step1:显示路径下文件信息，指定正常路径;expect:显示成功-----"
        self.logger.info(step1)
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.",
                      execute_msg, "获取卷组名失败")
        vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(vgname)
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"dsscmd ls -p +{vgname}; dsscmd ls -p +{vgname}/pg_xlog0"
        self.logger.info(cmd)
        res = self.common.get_sh_result(self.primary_node, cmd)
        self.assertIn("Succeed to ls dir info", res, "执行失败" + step1)
        self.assertIn("Succeed to ls link info", res.splitlines()[-1],
                      "执行失败" + step1)

        step2 = "-----step2:显示路径下文件信息，指定错误路径;expect:合理报错-----"
        self.logger.info(step2)
        cmd_list = [f'+{vgname}1', f'+{vgname}#', f' {vgname}']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd ls -p {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            if cmd == f'+{vgname}1':
                self.assertIn("Failed to find vg when ls the path +data1",
                              execute_msg, "执行失败" + step2)
            else:
                self.assertIn(f"cmd ls error:2193 Path {cmd[1::]} decode error",
                              execute_msg, "执行失败" + step2)

        step3 = "-----step3:显示路径下文件信息，不指定-p path参数;expect:合理报错-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd ls "
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("args [-p|--path] needs input value",
                      execute_msg, "执行失败" + step3)

        step4 = "-----step4:显示路径下文件信息，指定-m measure_type参数为正常值;expect:显示成功-----"
        self.logger.info(step4)
        cmd_list = ['-m B', '-m K', '-m M', '-m G', '-m T']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                          f"dsscmd ls -p +{vgname} {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(self.primary_node,
                                                    execute_cmd)
            self.assertIn("Succeed to ls dir", execute_msg, "执行失败" + step4)

        step5 = "-----step5:显示路径下文件信息，指定-m measure_type参数为非法值;" \
                "expect:合理报错measure_type error.-----"
        self.logger.info(step5)
        cmd_list = ['-m A', '-m b', '-m g']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                          f"dsscmd ls -p +{vgname} {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(
                self.primary_node, execute_cmd)
            self.assertIn("measure_type error.", execute_msg,
                          "执行失败" + step5)

        step6 = "-----step6:显示路径下文件信息，指定-U UDS:socket_domain参数" \
                "为正常值;expect:显示成功-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd ls -p +{vgname} -U {macro.UDS_PATH}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to ls dir info", execute_msg, "执行失败" + step6)

        step7 = "-----step7:显示路径下文件信息，指定指定错误的" \
                "-U UDS:socket_domain参数和权限不足的socket_domain;" \
                "expect:合理报错Failed to get uds connection.-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd ls -p +{vgname} -U {macro.UDS_PATH}.cfg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step6)
        execute_cmd = f"chmod 000 {macro.UDS_PATH[4::]};" \
                      f"source {macro.DB_ENV_PATH};" \
                      f"dsscmd ls -p +{vgname} -U {macro.UDS_PATH};" \
                      f"chmod 600 {macro.UDS_PATH[4::]};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step7)

        step8 = "-----step8:显示路径下文件信息，同时指定-m,-U参数;expect:显示成功-----"
        self.logger.info(step8)
        cmd_list = [f'-m B -U {macro.UDS_PATH}', f'-m K -U {macro.UDS_PATH}']
        for cmd in cmd_list:
            execute_cmd = f"source {macro.DB_ENV_PATH};" \
                          f"dsscmd ls -p +{vgname} {cmd}"
            self.logger.info(execute_cmd)
            execute_msg = self.common.get_sh_result(
                self.primary_node, execute_cmd)
            self.assertIn("Succeed to ls dir info", execute_msg,
                          "执行失败" + step8)

        step9 = "-----step9:在集群停止的情况下执行;expect:合理报错-----"
        self.logger.info(step9)
        stop_msg = self.primary_sh.exec_cm_ctl(mode='stop')
        self.logger.info(stop_msg)
        self.assertIn(self.constant.cm_stop_success_msg,
                      stop_msg, '执行失败' + step9)
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd ls -p +{vgname}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.constant.failed_to_get_uds_connection,
                      execute_msg, "执行失败" + step9)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        start_msg = self.primary_sh.exec_cm_ctl(mode='start')
        self.logger.info(start_msg)
        status = self.primary_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.logger.info(status)
        self.assertIn(self.constant.cm_start_success_msg,
                      start_msg, '启动集群失败')
        self.assertIn('cluster_state   : Normal', status, '执行失败')
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
